package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.DynamicMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData)
    {
        return this.transform(dictDataMapper.selectDictDataList(dictData));
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     * 
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     * 
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    // 将SQL类型的字典转换为
    private List<SysDictData> transform(List<SysDictData> list){
        AtomicReference<String> tableName=new AtomicReference<>(null),
                labelField=new AtomicReference<>(null),
                dictType=new AtomicReference<>(null),
                sourceType=new AtomicReference<>(null),
                valueField=new AtomicReference<>(null);


        list.stream().filter(t->t.getDictLabel().toString().toLowerCase(Locale.ROOT).equals("sourcetype")).findFirst().ifPresent(t->sourceType.set(t.getDictValue()));

        if(StringUtils.isEmpty(sourceType.get())) {
            return list;
        }

        //list.stream().filter(t->t.getDictCode().toString().toLowerCase(Locale.ROOT).equals("sourcetype")).findFirst().ifPresent(t->sourceType.set(t.getDictValue()));
        list.stream().filter(t->t.getDictLabel().toString().toLowerCase(Locale.ROOT).equals("tablename")).findFirst().ifPresent(t->tableName.set(t.getDictValue()));
        list.stream().filter(t->t.getDictLabel().toString().toLowerCase(Locale.ROOT).equals("valuefield")).findFirst().ifPresent(t->valueField.set(t.getDictValue()));
        list.stream().filter(t->t.getDictLabel().toString().toLowerCase(Locale.ROOT).equals("labelfield")).findFirst().ifPresent(t->labelField.set(t.getDictValue()));
        list.stream().filter(t->t.getDictLabel().toString().toLowerCase(Locale.ROOT).equals("dicttype")).findFirst().ifPresent(t->dictType.set(t.getDictValue()));

        List<SysDictData> result = new ArrayList<>();
        if(sourceType.get().equals("SQL")) {
            String sql = "SELECT ".concat(valueField.get()).concat(" as 'dictValue', ").concat(labelField.get()).concat(" as 'dictLabel' FROM ").concat(tableName.get());

            List<LinkedHashMap<String, Object>> list1 = dynamicMapper.query(sql, null);
            if (CollectionUtils.isNotEmpty(list1)) {
                list1.forEach(t -> {
                    SysDictData sysDictData = new SysDictData();
                    sysDictData.setDictType(dictType.get());
                    sysDictData.setDictSort(-1L);
                    sysDictData.setDictCode(-1L);
                    sysDictData.setIsDefault("0");
                    sysDictData.setStatus("0");
                    sysDictData.setDictLabel(String.valueOf(t.get("dictLabel")));
                    sysDictData.setDictValue(String.valueOf(t.get("dictValue")));
                    result.add(sysDictData);
                });
            }
        }
        return result;
    }

    @Autowired
    DynamicMapper dynamicMapper;

}
